<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Controller;
use think\Request;
use think\Lang;
use fast\Tree;
use fast\Cols;
use fast\Date;
use fast\Random;
use think\cache\Driver\File;

class Modulestock extends Backend{
		protected $model = null;//模型对象
		protected $columnlist = array();
		public function _initialize(){
				parent::_initialize();
				$this->model = model('Modulestock');
				//$this->columnarry = Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/module.php','',1);
		}

		public function index(){
			$columnlist = Cols::columnread('modulestock');
			//设置过滤方法
			$this->request->filter(['strip_tags']);
			//如果发送的来源是Selectpage，则转发到Selectpage
			if ($this->request->request('pkey_name')){
					return $this->selectpage();
			}
			$searchfileds = array();
			$searchfileds['versiontitle'] = 'varcharmid';
			$searchfileds['moduleid'] = 'varcharmid';
			list($where, $sort, $order, $offset, $limit) = $this->buildparams($searchfileds);
			$order = 'desc';
			$sort = 'createtime';
			$total = $this->model
							->where($where)
							->order($sort, $order)
							->count();
			$totalpage = 1;$page = 1;
			if($limit>0){
					$totalpage = $total%$limit==0?intval($total/$limit):intval($total/$limit)+1;
					//总页数
					if($offset>0){
							$page = intval($offset/$limit)+1;
					}
					//当前页
			}
			$list = $this->model
							->where($where)
							->order($sort, $order)
							->limit($offset, $limit)
							->select();
			//print_r(json_encode($list));exit;
			$arealist = array();$citylist = array();
			for ($i = 0; $i < count($list); $i++) {
					//$list[$i]['catname'] = $list[$i]['category']['name'];
					if(isset($list[$i]['createtime'])){
							$list[$i]['createdate'] = Date::timetodate($list[$i]['createtime'],3);
					}
					if(isset($list[$i]['updatetime'])){
							$list[$i]['updatedate'] = Date::timetodate($list[$i]['updatetime'],3);
					}
					unset($list[$i]['intro']);unset($list[$i]['content']);
			}
			$result = array("total" => $total, "rows" => $list);
			$this->view->assign("lists",$result['rows']);
			$this->view->assign("columnlist",$columnlist);
			$this->view->assign("total",$result['total']);
			$this->view->assign("offset",$offset);
			$this->view->assign("limit",$limit);
			$this->view->assign("totalpage",$totalpage);
			$this->view->assign("page",$page);
			return $this->view->fetch('index');
		}
		public function info(){
				try{
						$soap = new \SoapClient(null,array(
						"location" => "http://www.andiff.net/index/modulestock/info",
						"uri"      => "abcd",  //资源描述符服务器和客户端必须对应
						'trace' 	 => true,
						'encoding' =>'utf-8'));
						//$soap->__soapCall('querySherry',$paras);
						$s = $soap->info($this->request->param('ids'));
						//$arr = var_dump($s->relation);
						$arr = $s->relation;

						/*
						print_r(json_encode($arr['relation']['stocks'][0]->data));exit;
						[itemid] => cde39283e691429ea3786834ef892c66 [dir] => [name] => 测试
						[enname] => test [pid] => [catid] => 0 [intro] =>测试测试
						[createtime] => 1522918384 [updatetime] => 1522918384
						*/
						//$arr2 = get_object_vars($arr['relation']['stocks']);
						//print_r($arr['stocks']);exit;

						$arr2 = array();
						foreach ($arr['stocks'] as $k => $v) {
								$arr2[$k]=get_object_vars($v);
						}
						//print_r($arr2);exit;
						$this->view->assign("s2",$arr2);
						$this->view->assign("s",$s->data);
						return $this->view->fetch();
				}catch(Exction $e){
				 		echo print_r($e->getMessage(),true);
				}
		}

		public function add(){
        if ($this->request->isPost()){
            $params = $this->request->post("row/a");
            if ($params){
                $this->modelValidate = false;
                if ($this->dataLimit){
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try{
                    //是否采用模型验证
                    if ($this->modelValidate){
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $params['itemid'] = str_replace('-','',Random::uuid());
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false){
                        $this->success();
                    }else{
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e){
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }else{
            $moduleid = $this->request->get("moduleid");
            $this->view->assign("moduleid",$moduleid?$moduleid:0);
            if($moduleid){
                $m = model('Module')->get(['itemid'=>$moduleid]);
                $cname = $m['enname'];
            }else{
                $cname = '';
            }
            $this->view->assign("cname",$cname);
            return $this->view->fetch();
        }
    }

}?>
